package com.fjnldx.mes.service.energy;

import com.fjnldx.mes.domain.domainRes.QueryResult;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.energy.req.LSSJReq;
import com.fjnldx.mes.domain.energy.req.SSCXReq;
import com.fjnldx.mes.domain.energy.vo.*;
import com.fjnldx.mes.mapper.energy.EnergyMapper;
import com.fjnldx.mes.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class EnergyService {

    @Resource
    EnergyMapper mapper;


    public Result queryKongTiao() {
        Result result = new Result();
        String gongxu = "空调";
        return getResult(result, gongxu);
    }

    public Result queryKongYa() {
        Result result = new Result();
        String gongxu = "空压";
        return getResult(result, gongxu);
    }

    private Result getResult(Result result, String gongxu) {
        List<KtKy_MemberVO> results = mapper.queryKTKYHD(gongxu);

        KtKy_sumVO sum = new KtKy_sumVO();
        double zhdl = 0, dbhd = 0, dthd = 0, dyhd = 0, dzhd = 0, dl = 0;
        for (KtKy_MemberVO code : results) {
            code.setDzhd(code.getDzhd() + code.getDthd());//当周 = dian_table + dian_day_summary
            code.setDyhd(code.getDyhd() + code.getDthd());//当月 = dian_table + dian_day_summary
            zhdl += code.getZhd();
            dbhd += code.getDbhd();
            dthd += code.getDthd();
            dyhd += code.getDyhd();
            dzhd += code.getDzhd();
            dl += code.getAvgI();
        }
        DecimalFormat dc = new DecimalFormat("0.0");
        sum.setZhdl(dc.format(zhdl));
        sum.setDbhdSum(dc.format(dbhd));
        sum.setDthdSum(dc.format(dthd));
        sum.setDyhdSum(dc.format(dyhd));
        sum.setDzhdSum(dc.format(dzhd));
        sum.setDlSum(dc.format(dl));

        KtKyVO ktKyVO = new KtKyVO();
        ktKyVO.setList(results);
        ktKyVO.setSum(sum);

        if (results.size() > 0) {
            result.success(ktKyVO);
        } else {
            result.error();
        }
        return result;
    }

    public Result querySSCX(SSCXReq req) {

        //根据班次 查询 细纱工序下的品种耗电百分比
        List<SSCX_PJ_VO> xSlist = mapper.queryXiShaByBC(req.getBc());
        //根据班次 查询 络筒工序下的品种耗电百分比
        List<SSCX_PJ_VO> Ltlist = mapper.queryLuoTongByBC(req.getBc());

        Date start = req.getStart();
        Date end = req.getEnd();

        List<SSCX_GongXu_VO> gonXuHd = new ArrayList<>();
        if (!StringUtils.isEmpty(req.getRange())) {
            end = new Date();
            if ("day".equals(req.getRange())) { //获取当天的耗电
                gonXuHd = mapper.getDayGongGXHD(req.getBc());
            } else if ("week".equals(req.getRange())) { //获取当周的耗电
                start = DateUtil.getMonday(end);
                gonXuHd = mapper.getDaySummaryGongGXHD(start, end);
            } else if ("month".equals(req.getRange())) { //获取当月的耗电
                start = DateUtil.getMonth(end);
                gonXuHd = mapper.getDaySummaryGongGXHD(start, end);
            }
        } else {
            if (null != start && null != end) {
                gonXuHd = mapper.getDaySummaryGongGXHD(start, end);
            }
        }


        SSCXVO data = new SSCXVO();
        data.setXiShaPjHD(xSlist);
        data.setLuoTongPjHD(Ltlist);
        data.setGongXuHD(gonXuHd);

        return Result.success(data);
    }

    public Result queryCjAndGongXuList() {
        LSCX_CJ_GX_VO result = new LSCX_CJ_GX_VO();
        List<String> list = mapper.getSCCJ();
        List<Map<String, String>> cjList = new ArrayList<>();
        for (String s : list) {
            String[] cj = s.split("--");
            Map<String, String> mp = new HashMap<>();
            mp.put(cj[0], cj[1]);
            cjList.add(mp);
        }
        result.setCj(cjList);
        result.setGx(mapper.getGongXu());
        return Result.success(result);

    }

    public QueryResult queryHistory(LSSJReq req) {
        QueryResult result = new QueryResult();
        if (null == req.getPage() || null == req.getPageSize()) {
            req.setPage(null);
            req.setPageSize(null);
        } else {
            req.setPage((req.getPage() - 1) * req.getPageSize());
            req.setPageSize((req.getPage() + req.getPageSize()));
        }
        if (StringUtils.isEmpty(req.getCj())) {
            req.setCj(null);

        }
        if (StringUtils.isEmpty(req.getGx())) {
            req.setGx(null);

        }
        if (StringUtils.isEmpty(req.getSn())) {
            req.setSn(null);
        }
        List<LSCX_VO> list = mapper.queryHistory(req);
        Integer total = mapper.queryHistoryCount(req);
        result.setCode(1);
        result.setMsg("查询成功");
        result.setStatus("success");
        result.setTotal(total);
        result.setData(list);

        return result;
    }

}
